package com.bootdo.system.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.bootdo.system.domain.VeriCode;

@Mapper
public interface VeriCodeMapper {

	@Select("select * from tb_vericode where id = #{id}")
	VeriCode get(Long id);
	
	@Select("select * from tb_vericode where phone = #{phone} limit 1")
	VeriCode getByPhone(String phone);
	
	@Insert("insert into tb_vericode (`phone`, `code`, `codeType`, `createTime`)"
	+ "values (#{phone}, #{code}, #{codeType}, #{createTime})")
	@Options(useGeneratedKeys=true)
	int save(VeriCode code);
	
	@Delete("delete from tb_vericode where id =#{id}")
	int remove(Long id);
	
	@Delete("delete from tb_vericode where phone =#{phone}")
	int removeByPhone(String phone);
	
	@Select("<script>" +
	"select * from tb_vericode " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"phone != null and phone != ''\">"+ "and phone = #{phone} " + "</if>" + 
	  		  "<if test=\"code != null and code != ''\">"+ "and code = #{code} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<VeriCode> list(Map<String,Object> map);
	
	
	@Update("<script>"+ 
			"update tb_vericode " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"phone != null\">`phone` = #{phone}, </if>" + 
                    "<if test=\"code != null\">`code` = #{code}, </if>" + 
                    "<if test=\"codeType != null\">`codeType` = #{codeType}, </if>" + 
                    "<if test=\"createTime != null\">`createTime` = #{createTime}, </if>" + 
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(VeriCode bComments);
}
